package com.innovative.androiddemo1;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.database.Cursor;

public class MainActivity extends Activity {
	Button btn;
	TextView txt;
	WebServices wb;
	C fun;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		txt = (TextView) findViewById(R.id.txt);
		btn = (Button) findViewById(R.id.button1);
		wb = new WebServices(this);
		fun = new C(this);
		btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// LoginTest();
				// GetServerTime();
				// Sync();
				// GetRegisteredData();
				// Register();
				ParseJSON();
				insertDB();
			}
		});
		// copydb();
		// DBAdapter db = new DBAdapter(this);
		// db.open();
		// Cursor cr = db.getDetails();
		// Log.v("count", cr.getCount() + "");
		// if (cr.getCount() > 0) {
		// String d = cr.getString(1);
		// Log.v("data", d);
		// txt.setText(d);
		// }
		// cr.close();
		// db.close();
	}

	private void insertDB() {
		DBAdapter db = new DBAdapter(this);
		db.open();
		long temp = db.addDrafts();
		Log.v("temp", temp + "");
		db.close();
	}

	private void displayDB() {
		DBAdapter db = new DBAdapter(this);
		db.open();
		Cursor cr = db.getDetails();
		Log.v("count", cr.getCount() + "");
		if (cr.getCount() > 0) {
			String d = cr.getString(1);
			Log.v("data", d);
			txt.setText(d);
		}
		cr.close();
		db.close();
	}

	private void ParseJSON(String json) {
		fun.ParseJSN(json);
	}

	private void ParseJSON() {
		String json = fun.jdata("jsondata.txt");
		// txt.setText(json);
		fun.ParseJSN(json);
	}

	public void GetServerTime() {
		String time = wb.ServerTime();
		txt.setText(time);
	}

	public void LoginTest() {
		String login = wb.Login("1234", "test", "");
		txt.setText(login);
	}

	public void Sync() {
		String lastSync = "", count = "", skip = "";
		String json = wb.Syncr(lastSync, count, skip);
		txt.setText(json);
		ParseJSON(json);
	}

	public void GetRegisteredData() {
		String data = wb.RegisteredData();
		txt.setText(data);
	}

	public void Register() {
		String rg = wb.Registration();
		txt.setText(rg);
	}
	// public void copydb() {
	// DataBaseHelper myDbHelper;
	// myDbHelper = new DataBaseHelper(MainActivity.this);
	// try {
	// myDbHelper.createDataBase();
	// } catch (IOException ioe) {
	// throw new Error("Unable to create database");
	// }
	// }
}
